package com.grouuu.factory
{
	import com.grouuu.tile.TileGraphic;
	import flash.display.Bitmap;
	import flash.utils.Dictionary;
	import starling.textures.Texture;
	import starling.textures.TextureAtlas;
	
	/**
	 * ...
	 * @author Grouuu
	 */
	public class SpriteFactory 
	{
		[Embed(source="../../../../media/spritesheets/tiles/render/texture.png")]
		protected var SpriteSheet:Class;
		
		[Embed(source = "../../../../media/spritesheets/tiles/render/texture.xml", mimeType = "application/octet-stream")]
		protected var Map:Class;
		
		//// TEST
		[Embed(source = "../../../../media/spritesheets/test/render.png")]
		protected var SpriteSheetTest:Class;
		
		[Embed(source = "../../../../media/spritesheets/test/texture.xml", mimeType = "application/octet-stream")]
		protected var MapTest:Class;
		
		protected var assetTest:Bitmap;
		protected var mapTest:XML;
		protected var atlasTest:TextureAtlas;
		protected var textureTest:Texture;
		////
		
		protected var asset:Bitmap;
		protected var map:XML;
		protected var atlas:TextureAtlas;
		protected var texture:Texture;
		
		protected var bufferTextures:Array = [];
		protected var bufferTileGraphic:Dictionary = new Dictionary(false);
		
		public function SpriteFactory() 
		{
			asset = new SpriteSheet();
			map = XML(new Map());
			texture = Texture.fromBitmap(asset);
			atlas = new TextureAtlas(texture, map);
			
			//// TEST
			assetTest = new SpriteSheetTest();
			mapTest = XML(new MapTest());
			textureTest = Texture.fromBitmap(assetTest);
			atlasTest = new TextureAtlas(textureTest, mapTest);
			////
		}
		
		//// TEST
		public function getTileGraphicTest(idTile:int, col:int, row:int):TileGraphic
		{
			var tex:Texture;
			
			tex = atlasTest.getTexture("tile_" + col + "_" + row);
			
			var listTex:Vector.<Texture> = new Vector.<Texture>(1);
			listTex[0] = tex;
			
			return new TileGraphic(idTile, listTex);
		}
		////
		
		public function getTileGraphic(idTile:int):TileGraphic
		{
			if (bufferTileGraphic[idTile])
			{
				if (bufferTileGraphic[idTile].length > 0)
				{
					var tile:TileGraphic = bufferTileGraphic[idTile].shift();
					
					tile.start();
					
					return tile;
				}
			}
			
			var tex:Vector.<Texture>;
			
			if (!bufferTextures[idTile])
			{
				tex = atlas.getTextures(getTextureName(idTile));
				
				bufferTextures[idTile] = tex;
			}
			else
				tex = bufferTextures[idTile];
			
			return new TileGraphic(idTile, tex);
		}
		
		protected function getTextureName(idTile:int):String
		{
			var idTexture:String;
			
			switch(idTile)
			{
				case 0: idTexture = "tile_barrier_horizon"; break;
				case 1: idTexture = "tile_barrier_horizon"; break;
				case 2: idTexture = "tile_barrier_corner_top_right"; break;
				case 3: idTexture = "tile_barrier_right"; break;
				case 4: idTexture = "tile_barrier_corner_bot_right"; break;
				case 5: idTexture = "tile_barrier_corner_bot_left"; break;
				case 6: idTexture = "tile_barrier_left"; break;
				case 7: idTexture = "tile_barrier_corner_top_left"; break;
				case 8: idTexture = "tile_grass"; break;
			}
			
			return idTexture;
		}
		
		public function deleteTileGraphic(tile:TileGraphic):void
		{
			if (!bufferTileGraphic[tile.idTile])
				bufferTileGraphic[tile.idTile] = new Vector.<TileGraphic>();
			
			tile.reset();
			
			bufferTileGraphic[tile.idTile].push(tile);
		}
	}
}